package com.tomato.joke;

import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.tomato.joke.service.JokeTopicService;
import com.tomato.joke.task.RefreshMainTask;

public class MainActivity extends BaseActivity {

	private ListView listview;
	private ImageView refreshImageView;
	private boolean autoLoading = false; // 是否自动加载更多内容
	private ImageView loadingView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		initFooterEvent();

		initControl();
	}

	/**
	 * 初始化控件
	 */
	private void initControl() {
		JokeTopicService jokeTopicService = new JokeTopicService(this);
		List<Map<String, Object>> topics = jokeTopicService.queryNewTopics();
		SimpleAdapter adapter = new SimpleAdapter(this, topics,
				R.layout.main_list_item, new String[] { "thumbnail", "content",
						"id" }, new int[] { R.id.list_item_icon,
						R.id.list_item_content, R.id.list_item_id });

		loadingView = (ImageView) findViewById(R.id.load_button);
		listview = (ListView) findViewById(R.id.main_list_item);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				redirectDetailActivity(v);

			}
		});

		listview.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (autoLoading && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					loadingView.setVisibility(View.GONE);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			}
		});

		listview.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if (autoLoading && loadingView == view) {
					loadingView.setVisibility(View.GONE);
					listview.setSelection(position - 1);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});

		refreshImageView = (ImageView) findViewById(R.id.refresh_image);
		refreshImageView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new RefreshMainTask(refreshImageView, listview).execute();
			}
		});

		loadingView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				autoLoading = true;
				new RefreshMainTask(refreshImageView, listview).execute();
			}
		});
	}

	/**
	 * 查看详情
	 * 
	 * @param v
	 */
	private void redirectDetailActivity(View v) {
		try {
			Intent intent = new Intent();
			// 传递参数
			intent.setClass(MainActivity.this, TopicDetailActivity.class);
			TextView contentView = (TextView) (v.findViewById(R.id.list_item_content));
			TextView idView = (TextView) (v.findViewById(R.id.list_item_id));

			String content = contentView.getText().toString();
			int id = Integer.parseInt(idView.getText().toString());

			Bundle bundle = new Bundle();
			bundle.putString("content", content);
			bundle.putInt("id", id);

			Log.d("topic id", String.valueOf(id));
			intent.putExtras(bundle);

			startActivityForResult(intent, 0);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}